
package com.wyzz.hr.bo.impl;

import java.util.*;
import org.hibernate.FetchMode;
import org.hibernate.criterion.*;

import com.wyzz.hr.base.ComonBeans;
import com.wyzz.hr.base.wf.AbstractWorkflow;
import com.wyzz.hr.bo.ISysLogBO;
import com.wyzz.hr.dao.ISysLogDAO;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Leaverequest;
import com.wyzz.hr.pojo.Syslog;
import com.wyzz.hr.util.MyTools;


public class SysLogBOImpl implements ISysLogBO {

	public SysLogBOImpl() {
		logDAO = null;
	}

	public void deleteLogs(String tableName, String key) {
		List results = getLogs(tableName, key);
		if (results != null && results.size() > 0)
			logDAO.deleteCollection(results);
	}

	public List getLogs(String tableName, String key) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Syslog.class);
		dc.setFetchMode("slChangeEmpno", FetchMode.JOIN);
		dc.setFetchMode("slChangeEmpno.empBuNo", FetchMode.JOIN);
		dc.setFetchMode("slChangeEmpno.empLocationNo", FetchMode.JOIN);
		dc.setFetchMode("slChangeEmpno.empDeptNo", FetchMode.JOIN);
		dc.addOrder(Order.desc("slChangeTime"));
		dc.add(Restrictions.eq("slTableName", tableName.trim()));
		dc.add(Restrictions.eq("slRecordId", key.trim()));
		List logs = logDAO.findByCriteria(dc);
		Iterator i$ = logs.iterator();
		do {
			if (!i$.hasNext())
				break;
			Syslog log = (Syslog) i$.next();
			if (log.getSlType().intValue() == 1)
				log.setSlComments((String) log.getDecry("SlComments", log
						.getSlRecordId(), MyTools.CHINESE));
		} while (true);
		return logs;
	}

	public List insertLog(Syslog log, String empNo) {
		log.setSlChangeTime(new Date());
		log.setSlChangeEmpno(new Employee(empNo));
		logDAO.saveObject(log);
		return null;
	}

	public List insertLog(Syslog log) {
		log.setSlChangeTime(new Date());
		logDAO.saveObject(log);
		return null;
	}

	public void addToSyslog(String tableName, String changeEmpno, String empNo,
			String recordId, int type, String action, String comments) {
		Syslog sl = new Syslog();
		sl.setSlTableName(tableName);
		if (changeEmpno == null)
			sl.setSlChangeEmpno(null);
		else
			sl.setSlChangeEmpno(new Employee(changeEmpno));
		sl.setSlEmpno(empNo);
		sl.setSlRecordId(recordId);
		sl.setSlType(Integer.valueOf(type));
		sl.setSlAction(action);
		sl.setSlComments(comments);
		if (sl.getSlType().intValue() == 1)
			sl.setEncry("SlComments", sl.getSlComments(), sl.getSlRecordId(),
					MyTools.CHINESE);
		insertLog(sl);
	}

	public void addLog(List objs) {
		List logList = new ArrayList(objs.size());
		AbstractWorkflow wf = null;
		Syslog log;
		for (Iterator i$ = objs.iterator(); i$.hasNext(); logList.add(log)) {
			Object obj = i$.next();
			log = new Syslog();
			log.setSlType(Integer.valueOf(0));
			wf = AbstractWorkflow.getWorkflowAndSyslog(obj, log);
			log.setSlTableName(wf.getFlowType());
			log.setSlComments(wf.getComment());
			log.setSlChangeTime(wf.getOpTime());
			log.setSlChangeEmpno(new Employee(new String(wf.getOperator()
					.getId())));
			log.setSlAction(ComonBeans.getParameterValue("lrStatus",
					new String[] { wf.getStatus().toString() }));
			if (wf.getApplier() != null)
				log.setSlEmpno(new String(wf.getApplier().getId()));
		}

		logDAO.saveOrupdate(logList);
	}

	public ISysLogDAO getLogDAO() {
		return logDAO;
	}

	public void setLogDAO(ISysLogDAO logDAO) {
		this.logDAO = logDAO;
	}

	public void saveOrupdate(Collection objs) {
		logDAO.saveOrupdate(objs);
		Iterator i$ = objs.iterator();
		do {
			if (!i$.hasNext())
				break;
			Object o = i$.next();
			if (o instanceof Leaverequest) {
				Leaverequest lr = (Leaverequest) o;
				if (lr.getUpdateOvertimerequestList() != null)
					logDAO.saveOrupdate(lr.getUpdateOvertimerequestList());
				if (lr.getUpdateOvertimetxusedList() != null)
					logDAO.saveOrupdate(lr.getUpdateOvertimetxusedList());
				if (lr.getDeleteOvertimetxUsedList() != null)
					logDAO.deleteCollection(lr.getDeleteOvertimetxUsedList());
			}
		} while (true);
	}

	public List exeHqlList(String hql) {
		return logDAO.exeHqlList(hql);
	}

	private ISysLogDAO logDAO;
}
